package com.example.admin.brainwave.net;

import android.app.Activity;
import android.text.TextUtils;

import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.bean.TesteeInfo;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.ITaskCallbackListener;
import com.example.admin.brainwave.utils.JsonUtils;
import com.example.admin.brainwave.utils.OkHttpUtil;
import com.example.admin.brainwave.utils.ProgressDialogManager;
import com.example.admin.brainwave.utils.RxTask;
import com.example.admin.brainwave.utils.SPHelper;

import java.util.HashMap;
import java.util.Map;


public class SaveTesteeInfoTask extends RxTask<String, Integer, CurrencyBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public SaveTesteeInfoTask(Activity activity,
                              ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected CurrencyBean doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Const.SAVE_PERSONAL_INFOR, getParam(params[0], params[1], params[2], params[3], params[4], params[5], params[6]));
        if (!TextUtils.isEmpty(result)) {
            CurrencyBean currencyBean = JsonUtils.parseObject(result, CurrencyBean.class);
            return currencyBean;
        }
        return null;
    }

    @Override
    protected void onPostExecute(CurrencyBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private Map<String, Object> getParam(String fullName, String gender, String age, String nationCode, String education, String profession, String diseaseHistory) {
        TesteeInfo testeeInfo = new TesteeInfo();
        testeeInfo.setId(SPHelper.getInstance(activity).getString("userId", ""));
        testeeInfo.setFullName(fullName);
        testeeInfo.setGender(gender);
        testeeInfo.setAge(Integer.parseInt(age));
        testeeInfo.setNationCode(nationCode);
        testeeInfo.setEducationCode(education);
        testeeInfo.setProfession(profession);
        testeeInfo.setDiseaseHistory(diseaseHistory);
        Map<String, Object> map = new HashMap<>();
        map.put("testeeInfo", testeeInfo);
        return map;
    }
}